package com.woniu.zsd.recycle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.zsd.common.util.Result;
import com.woniu.zsd.platform.FeignClient.StationFeignClient;
import com.woniu.zsd.platform.form.UpdateSiteScoreBySiteIdForm;
import com.woniu.zsd.recycle.dto.SelectEvaluateByUserIdDto;
import com.woniu.zsd.recycle.dto.StationScoreDto;
import com.woniu.zsd.recycle.mapper.RecyclingOrderMapper;
import com.woniu.zsd.recycle.model.Evaluate;
import com.woniu.zsd.recycle.mapper.EvaluateMapper;
import com.woniu.zsd.recycle.model.RecyclingOrder;
import com.woniu.zsd.recycle.param.UserEvaluateParam;
import com.woniu.zsd.recycle.service.EvaluateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 彭于晏
 * @since 2025年02月18日
 */
@Service
public class EvaluateServiceImpl extends ServiceImpl<EvaluateMapper, Evaluate> implements EvaluateService {
    @Resource
    private EvaluateMapper evaluateMapper;
    @Resource
    private RecyclingOrderMapper recyclingOrderMapper;
    @Resource
    private StationFeignClient stationFeignClient;
    @Resource
    private RedissonClient redissonClient;

    @Override
    public void userEvaluate(UserEvaluateParam param) throws Exception {
        RLock lock = redissonClient.getFairLock(param.getOrderNum());
        try {
            lock.lock();
            Evaluate evaluate = new Evaluate();
            evaluate.setUserId(param.getUserId());
            evaluate.setOrderNum(param.getOrderNum());
            evaluate.setScore(param.getScore());
            evaluate.setEvaluateContent(param.getEvaluateContent());
            evaluate.setState(1);//1.代表已评价，2.代表已删除3.代表以回复
            evaluate.setEvaluateTime(LocalDateTime.now());
            evaluate.setStationId(param.getStationId());
            //调用平台服务得到站点名称
            Result result = stationFeignClient.selectStationNameByStationId(param.getStationId());
            String name = (String) result.getData();
            evaluate.setStationName(name);
            evaluateMapper.insert(evaluate);
            //还要将订单表改为已评价状态
            RecyclingOrder order = recyclingOrderMapper.selectById(param.getOrderNum());
            order.setStatus(5);//订单表中5代表已评价或是已结束
            recyclingOrderMapper.updateById(order);
        }finally {
            lock.unlock();
        }
    }

    @Override
    public void siteReply(String orderNum, String stationReply) throws Exception {
        QueryWrapper<Evaluate> wrapper = new QueryWrapper<>();
        wrapper.eq("order_num", orderNum);
        Evaluate evaluate = evaluateMapper.selectOne(wrapper);
        evaluate.setStationReply(stationReply);
        evaluate.setState(3);//3.代表已回复
        evaluateMapper.updateById(evaluate);
    }

    @Override
    public void selectStationScore() throws Exception {
        List<StationScoreDto> stationScoreDtos = evaluateMapper.selectStationScore();
        for (StationScoreDto Dto : stationScoreDtos) {
            //将评分改为保留小数点后一位
            BigDecimal score = Dto.getAverageScores().setScale(1, BigDecimal.ROUND_HALF_UP);
            UpdateSiteScoreBySiteIdForm form = new UpdateSiteScoreBySiteIdForm();
            form.setStationId(Dto.getStationId());
            form.setRating(score);
            stationFeignClient.updateSiteScoreBySiteId(form);
        }

    }

    @Override
    public List<SelectEvaluateByUserIdDto> selectEvaluateByUserId(Integer userId) throws Exception {
        List<SelectEvaluateByUserIdDto> dtos = evaluateMapper.selectEvaluateByUserId(userId);
        return dtos;
    }

    @Override
    public void cancelEvaluate(String orderNum, Integer userId) throws Exception {
        QueryWrapper<Evaluate> wrapper = new QueryWrapper<>();
        wrapper.eq("order_num", orderNum);
        wrapper.eq("user_id", userId);
        Evaluate evaluate = evaluateMapper.selectOne(wrapper);
        evaluate.setState(2);//2.代表已删除
        evaluateMapper.updateById(evaluate);
    }
}
